package com.wanlian.common;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeType;

public class AddressManager
{
    public static void main(String[] args) throws IOException
    {
        String jsonString = readFile("E:\\holl7\\xrxproject\\trunk\\01 CI\\编码\\SERVER\\development\\xrx_util\\src\\main\\java\\com\\xrx\\util\\common\\address.json");
        ObjectMapper objectMapper = new ObjectMapper();

        // 使用ObjectMapper的readValue方法将json字符串解析到JsonNode实例中
        JsonNode rootNode = objectMapper.readTree(jsonString);
        // 直接从rootNode中获取某个键的值，
        // 这种方式在如果我们只需要一个长json串中某个字段值时非常方便
        Iterator<JsonNode> realElements = rootNode.elements();
        List<AddressTreeNode> addressTreeNodes = new ArrayList<>();
        while (realElements.hasNext())
        {
            JsonNode realElement = realElements.next();
            if (realElement.fieldNames().hasNext())
            {
                String name = realElement.fieldNames().next();
                AddressTreeNode addressTreeNode = new AddressTreeNode();
                addressTreeNode.setName(name);
                System.out.println(name);
                addressTreeNodes.add(addressTreeNode);
                getAddressNode(realElement, addressTreeNode);
            }

        }

        System.out.println("finish");

    }

    private static void getAddressNode(JsonNode node, AddressTreeNode addressTreeNode)
    {
        Iterator<JsonNode> realElements = node.elements();
        if (realElements.hasNext())
        {
            while (realElements.hasNext())
            {
                JsonNode realElement = realElements.next();
                if (realElement.getNodeType() == JsonNodeType.OBJECT)
                {
                    if (realElement.fieldNames().hasNext())
                    {
                        String name = realElement.fieldNames().next();
                        System.out.println(name);
                        AddressTreeNode cNode = new AddressTreeNode();
                        cNode.setName(name);
                        addressTreeNode.addChild(cNode);
                        getAddressNode(realElement, cNode);
                    }
                }
                else if (realElement.getNodeType() == JsonNodeType.STRING)
                {
                    String name = realElement.toString();
                    name = name.replaceAll("\"", "");
                    System.out.println(name);
                    AddressTreeNode cNode = new AddressTreeNode();
                    cNode.setName(name);
                    addressTreeNode.addChild(cNode);

                }
                else if (realElement.getNodeType() == JsonNodeType.ARRAY)
                {
                    getAddressNode(realElement, addressTreeNode);
                }
            }
        }

    }

    private static String readFile(String path) throws IOException
    {

        StringBuilder builder = new StringBuilder();

        try
        {

            InputStreamReader reader = new InputStreamReader(new FileInputStream(path), "UTF-8");
            BufferedReader bfReader = new BufferedReader(reader);

            String tmpContent = null;

            while ((tmpContent = bfReader.readLine()) != null)
            {
                builder.append(tmpContent);
            }

            bfReader.close();

        }
        catch (UnsupportedEncodingException e)
        {
            // 忽略
        }

        return filter(builder.toString());

    }

    // 过滤输入字符串, 剔除多行注释以及替换掉反斜杠
    private static String filter(String input)
    {

        return input.replaceAll("/\\*[\\s\\S]*?\\*/", "");

    }
}
